package com.baizhi.service.impl;

import com.baizhi.dao.AuctionDao;
import com.baizhi.dao.RecordDao;
import com.baizhi.dao.UserDao;
import com.baizhi.entity.Auction;
import com.baizhi.entity.User;
import com.baizhi.service.AuctionService;
import com.baizhi.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AuctionServiceImpl implements AuctionService {
    private SqlSession session = null;
    private AuctionDao dao = null;


    @Override
    public List<Auction> selectByPage(int pageNum, int pageSize) {
        List<Auction> list = null;
        try {
            session = MybatisUtil.openSession();
            dao = session.getMapper(AuctionDao.class);
            int offset = (pageNum-1)*pageSize;
            int rows = pageSize;
            list = dao.selectByPage(offset, rows);
            session.commit();
        } catch (Exception e) {
            if(session!=null){
                session.rollback();
            }
            throw new RuntimeException(e);
        }finally{
            if(session!=null){
                MybatisUtil.close(session);
            }
        }
        return list;
    }

    @Override
    public int count() {
        int totalPage = 0;
        try {
            session = MybatisUtil.openSession();
            dao = session.getMapper(AuctionDao.class);
            totalPage = dao.count();
            session.commit();
        } catch (Exception e) {
            if(session!=null){
                session.rollback();
            }
            throw new RuntimeException(e);
        }finally{
            if(session!=null){
                MybatisUtil.close(session);
            }
        }
        return totalPage;
    }

    @Override
    public void insert(Auction a) {
        try {
            session = MybatisUtil.openSession();
            dao = session.getMapper(AuctionDao.class);
            dao.insert(a);
            session.commit();
        } catch (Exception e) {
            if(session!=null){
                session.rollback();
            }
            throw new RuntimeException(e);
        }finally{
            if(session!=null){
                MybatisUtil.close(session);
            }
        }
    }

    @Override
    public List<Auction> selectFuzzy(String auctionName, String auctionDesc, String auctionStartTime, String auctionEndTime, double auctionUpset) {
        List<Auction> list = null;
        try {
            session = MybatisUtil.openSession();
            dao = session.getMapper(AuctionDao.class);
            list = dao.selectFuzzy(auctionName, auctionDesc, auctionStartTime, auctionEndTime, auctionUpset);
            session.commit();
        } catch (Exception e) {
            if(session!=null){
                session.rollback();
            }
            throw new RuntimeException(e);
        }finally{
            if(session!=null){
                MybatisUtil.close(session);
            }
        }
        return list;
    }

    @Override
    public void deleteById(int id) {
        try {
            session = MybatisUtil.openSession();
            RecordDao rDao = session.getMapper(RecordDao.class);
            rDao.deleteByAuctionId(id);
            this.dao = session.getMapper(AuctionDao.class);
            this.dao.deleteById(id);
            session.commit();
        } catch (Exception e) {
            if(session!=null){
                session.rollback();
            }
            throw new RuntimeException(e);
        }finally{
            if(session!=null){
                MybatisUtil.close(session);
            }
        }
    }

    @Override
    public void updateById(Auction a) {
        try {
            session = MybatisUtil.openSession();
            dao = session.getMapper(AuctionDao.class);
            dao.updateById(a);
            session.commit();
        } catch (Exception e) {
            if(session!=null){
                session.rollback();
            }
            throw new RuntimeException(e);
        }finally{
            if(session!=null){
                MybatisUtil.close(session);
            }
        }
    }

    @Override
    public Auction selectById(int id) {
        Auction a = null;
        try {
            session = MybatisUtil.openSession();
            dao = session.getMapper(AuctionDao.class);
            a = dao.selectById(id);
            session.commit();
        } catch (Exception e) {
            if(session!=null){
                session.rollback();
            }
            throw new RuntimeException(e);
        }finally{
            if(session!=null){
                MybatisUtil.close(session);
            }
        }
        return a;
    }
}
